package com.jusfoun.communication.dao.impl;

import com.jusfoun.communication.constant.AppInfoConstant;
import com.jusfoun.communication.constant.RedisKeyConstant;
import com.jusfoun.communication.dao.AppInfoDao;
import com.jusfoun.communication.model.AppInfo;
import com.jusfoun.communication.util.PageInfo;
import com.jusfoun.communication.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author player six
 * @email jslcylcy@163.com
 * @create 2017-12-12 16:25
 **/
@Component
public class AppInfoDaoImpl implements AppInfoDao {

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void add(AppInfo appInfo) {
        redisUtil.zAdd(RedisKeyConstant.REDIS_APPIDLIST, appInfo.getAppId(), System.currentTimeMillis());
        redisUtil.saveHashKey4Null(RedisKeyConstant.REDIS_APPINFO + appInfo.getAppId(), AppInfoConstant.REDIS_STORE_NAME, appInfo.getName());
        redisUtil.saveHashKey4Null(RedisKeyConstant.REDIS_APPINFO + appInfo.getAppId(), AppInfoConstant.REDIS_STORE_DESCRIPTION, appInfo.getDescription());
        redisUtil.saveHashKey4Null(RedisKeyConstant.REDIS_APPINFO + appInfo.getAppId(), AppInfoConstant.REDIS_STORE_APPSECRET, appInfo.getAppSecret());
    }

    @Override
    public Long appRank(String appId) {
        return redisUtil.zRank(RedisKeyConstant.REDIS_APPIDLIST, appId);
    }

    @Override
    public void update(AppInfo appInfo) {
        if (StringUtils.isNotBlank(appInfo.getName())) {
            redisUtil.saveHashKey4Null(RedisKeyConstant.REDIS_APPINFO + appInfo.getAppId(), AppInfoConstant.REDIS_STORE_NAME, appInfo.getName());
        }
        if (StringUtils.isNotBlank(appInfo.getDescription())) {
            redisUtil.saveHashKey4Null(RedisKeyConstant.REDIS_APPINFO + appInfo.getAppId(), AppInfoConstant.REDIS_STORE_DESCRIPTION, appInfo.getDescription());
        }
        if (StringUtils.isNotBlank(appInfo.getAppSecret())) {
            redisUtil.saveHashKey4Null(RedisKeyConstant.REDIS_APPINFO + appInfo.getAppId(), AppInfoConstant.REDIS_STORE_APPSECRET, appInfo.getAppSecret());
        }
    }

    @Override
    public long listIdCount() {
        return redisUtil.zSize(RedisKeyConstant.REDIS_APPIDLIST);
    }

    @Override
    public List<String> listId(PageInfo<AppInfo> appInfoPageInfo) {
        if (appInfoPageInfo.getStartRow() > 0 && appInfoPageInfo.getEndRow() >= appInfoPageInfo.getStartRow()) {
            return new ArrayList<>(redisUtil.zRangeByIdx(RedisKeyConstant.REDIS_APPIDLIST, appInfoPageInfo.getStartRow() - 1L, appInfoPageInfo.getEndRow() - appInfoPageInfo.getStartRow() + 1L, true));
        } else {
            return new ArrayList<>(redisUtil.zRangeByIdx(RedisKeyConstant.REDIS_APPIDLIST, 0, appInfoPageInfo.getTotal(), true));
        }
    }

    @Override
    public List<AppInfo> listAll() {
        long total = redisUtil.zSize(RedisKeyConstant.REDIS_APPIDLIST);
        if (total > 0) {
            Set<String> appIds = redisUtil.zRangeByIdx(RedisKeyConstant.REDIS_APPIDLIST, 0, total, true);
            List<AppInfo> list = new ArrayList<>();
            if (null != appIds && !appIds.isEmpty()) {
                for (String appId : appIds) {
                    list.add(getById(appId));
                }
            }
            return list;
        } else {
            return new ArrayList<>();
        }
    }

    @Override
    public AppInfo getById(String appId) {
        AppInfo appInfo = new AppInfo();
        appInfo.setAppId(appId);
        appInfo.setName(redisUtil.getHashKey(RedisKeyConstant.REDIS_APPINFO + appId, AppInfoConstant.REDIS_STORE_NAME));
        appInfo.setDescription(redisUtil.getHashKey(RedisKeyConstant.REDIS_APPINFO + appId, AppInfoConstant.REDIS_STORE_DESCRIPTION));
        return appInfo;
    }

    @Override
    public boolean checkAppIdAndSecret(String appId, String appSecret) {
        if (StringUtils.isBlank(appId) || StringUtils.isBlank(appSecret)) {
            return false;
        }
        return appSecret.equals(redisUtil.getHashKey(RedisKeyConstant.REDIS_APPINFO + appId, AppInfoConstant.REDIS_STORE_APPSECRET));
    }
}
